<?php
/* ******************************
 *	letterA Guarante List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');

$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);

        break;
    case 'get_edit_page':
	    $list_id	= $_REQUEST['id'];
		$page		= GetPage(GetGuaranteList($list_id));

		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $local_id	= $_REQUEST['local_id'];

	    $rResult = mysql_query("SELECT         warehouse_detail.id,
                                				production.`name`,
                                				warehouse_detail.barcode
                                FROM warehouse_detail
                                JOIN production ON production.id=warehouse_detail.prod_id
                                WHERE warehouse_detail.type=2 AND warehouse_detail.actived=1 AND warehouse_detail.warehouse_id='$warehouse_id'");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'save_guarante':
		$guarante_id		= $_REQUEST['id'];
		$local_id			= $_REQUEST['lid'];
		$production_id		= GetProductID($_REQUEST['cp']);

		$arr = array(
				"warehouse_id"	=> $_REQUEST['wid'],
				"production_id"	=> $production_id,
				"serial_number"	=> $_REQUEST['sn'],
				"comment"		=> htmlspecialchars($_REQUEST['sc'], ENT_QUOTES)
		);

		if($guarante_id == ''){
			AddGuaranteList($user_id, $local_id, $arr);
		}else{
			SaveGuaranteList($guarante_id, $user_id, $local_id, $arr);
		}

        break;
    case 'get_cartridge':
    	$barcode		= $_REQUEST['barcode'];
    	$client			= $_REQUEST['client'];
    	$data		= array('cartridge'	=> GetCartridge($barcode,$client) );

    	break;
    case 'disable':
		$list_id = $_REQUEST['id'];
		DisableGuaranteList($list_id);

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	letterA Guarante List Functions
 * ******************************
 */

function ChangeLocation($barcode){
	$result = mysql_query("	SELECT	`reserve`.`id`
							FROM 	`reserve`
							WHERE 	`reserve`.`barcode` = '$barcode'");

	$result = mysql_num_rows($result);

	if ( $result >= 1) {
		mysql_query("	UPDATE 	`reserve`
		SET 	`reserve`.`object_id` = '134',
		`reserve`.`production_condition` =  0
		WHERE	`reserve`.`barcode` = '$barcode' ");
	}else {
		$c_data = date('Y-m-d H:i:s');
		$prod_id 	= (int) substr($barcode, 5, 4);

		mysql_query("	INSERT INTO reserve
							(object_id, production_id, barcode, production_condition, quantity, create_date)
						VALUES
							('134', $prod_id, '$barcode', 0, 1, '$c_data')");
	}
}

function AddGuaranteList($user_id, $local_id, $arr)
{
	mysql_query("INSERT INTO	`warehouse_detail`
					(`user_id`, `warehouse_id`,`prod_id`,`barcode`,`type`)
				VALUES
					($user_id, '$arr[warehouse_id]','$arr[production_id]','$arr[serial_number]',2)");

	ChangeLocation($arr[serial_number]);
}

function SaveGuaranteList($guarante_id, $user_id, $local_id, $arr)
{

}

function DisableGuaranteList($list_id)
{
 mysql_query("UPDATE `warehouse_detail` SET `actived`='0' WHERE (`id`='$list_id')");
}

function GetProductID($prod_name)
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT	`id`
										  FROM		`production`
										  WHERE		`name` = '$prod_name'"));
	return $res['id'];
}

function GetGuaranteList($list_id)
{
    $res = mysql_fetch_assoc(mysql_query("SELECT warehouse_detail.id,
                                    				production.`name`,
                                    				warehouse_detail.barcode,
                                    				warehouse_detail.`comment`
                                    FROM   warehouse_detail
                                    JOIN   production ON production.id=warehouse_detail.prod_id
                                    WHERE warehouse_detail.id=$list_id"));
	return $res;
}

function GetCartridge($barcode,$client){
	$clientID = (int) substr($barcode, 1, 4);
	$cartridgeID = (int) substr($barcode, 5, 4);
	if($client == $clientID){
		$res = mysql_query("SELECT	`production`.`name`
						   FROM		`production`
						   WHERE	`production`.`id` = '$cartridgeID'");
		$res = mysql_fetch_assoc($res);
		return $res['name'];
	}else{
		global $error;
		$error = 'კლიენტი არასწორია!';
	}
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="guarante_name">დასახელება</label></td>
					<td>
						<div class="seoy-row" id="client_proruction_seoy">
							<input type="text" id="client_guarante_proruction" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="serial_number">შტრიხკოდი</label></td>
					<td>
						<input type="text" id="serial_number" type="text" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" style="width: 160px;"  value="' . $res['serial_number'] . '" />
					</td>
				</tr>
				<tr class="comment">
					<td style="width: 170px;" valign="top"><label for="guarante_comment">შენიშვნა</label></td>
					<td>
						<textarea id="guarante_comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
					</td>
				</tr>
			</table>
        </fieldset>
		<!-- ID -->
		<input type="hidden" id="guarante_id" value="' . $res['id'] . '" />
    </div>
    ';
	return $data;
}

?>